<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>书籍列表</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
    <h1 class="text-center">书籍列表</h1>

    <form class="mb-4" action="/books" method="get">
        <div class="input-group">
            <input type="text" name="search" class="form-control" placeholder="搜索书籍">
            <div class="input-group-append">
                <button class="btn btn-primary" type="submit">搜索</button>
            </div>
        </div>
    </form>

    <div class="text-center mb-4">
        <a href="/book/new" class="btn btn-success">添加书籍</a>
    </div>

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>书籍名称</th>
            <th>类型</th>
            <th>作者</th>
            <th>价格</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="book : ${books}">
            <td th:text="${book.name}">书名</td>
            <td th:text="${book.type}">类型</td>
            <td th:text="${book.author}">作者</td>
            <td th:text="${book.price}">价格</td>
            <td>
                <!-- 查看书籍 -->
                <a th:href="@{/book/{id}(id=${book.id})}" class="btn btn-info btn-sm">查看</a>
                <!-- 修改书籍 -->
                <a th:href="@{/book/edit/{id}(id=${book.id})}" class="btn btn-warning btn-sm">编辑</a>
                <!-- 删除书籍 -->
                <form th:action="@{/book/delete/{id}(id=${book.id})}" method="post" style="display:inline;">
                    <button class="btn btn-danger btn-sm" type="submit">删除</button>
                </form>
            </td>
        </tr>
        <tr th:if="${#lists.isEmpty(books)}">
            <td colspan="5" class="text-center">没有找到书籍</td>
        </tr>
        </tbody>
    </table>

    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            <li class="page-item" th:class="${currentPage == 1} ? 'disabled'">
                <a class="page-link"
                   th:href="@{/books(page=${currentPage - 1}, size=${size}, search=${search})}">上一页</a>
            </li>

            <li class="page-item" th:each="pageNum : ${#numbers.sequence(1, totalPages)}"
                th:class="${pageNum == currentPage} ? 'active'">
                <a class="page-link" th:href="@{/books(page=${pageNum}, size=${size}, search=${search})}"
                   th:text="${pageNum}"></a>
            </li>

            <li class="page-item" th:class="${currentPage == totalPages} ? 'disabled'">
                <a class="page-link"
                   th:href="@{/books(page=${currentPage + 1}, size=${size}, search=${search})}">下一页</a>
            </li>
        </ul>
    </nav>
    <div class="text-center mb-4">
        <p th:if="${search != null}">当前搜索: <strong th:text="${search}"></strong></p>
        <p>共 <strong th:text="${totalRecords}"></strong> 本书，当前页 <strong th:text="${currentPage}"></strong> /
            <strong th:text="${totalPages}"></strong></p>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
